package com.itsm.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itsm.user.entity.JzsyRbacUser;
import com.itsm.user.mapper.JzsyRbacUserMapper;
import com.itsm.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.PrivateKey;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;


@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private JzsyRbacUserMapper jzsyRbacUserMapper;

    public JzsyRbacUser register(String userName) {
        JzsyRbacUser jzsyRbacUser = new JzsyRbacUser();
        jzsyRbacUser.setUserName(userName);
        jzsyRbacUser.setPassword("E10ADC3949BA59ABBE56E057F20F883E");
        jzsyRbacUser.setUserType("0");
        jzsyRbacUser.setMainType("0");
        Date now = new Date();
        LocalDateTime localDateTime = now.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        jzsyRbacUser.setStartDate(localDateTime);
        jzsyRbacUser.setCreatedBy("一个用户");
        jzsyRbacUser.setCreationDate(localDateTime);
        jzsyRbacUser.setLastUpdatedBy("一个用户");
        jzsyRbacUser.setLastUpdateDate(localDateTime);

        jzsyRbacUserMapper.insert(jzsyRbacUser);


        return jzsyRbacUser;
    }

    public JzsyRbacUser getUserByName(String userName){
        QueryWrapper<JzsyRbacUser> wu = new QueryWrapper<>();
        wu.select("user_name ,password").eq("user_name",userName);
        List<JzsyRbacUser> lu = jzsyRbacUserMapper.selectList(wu);

        if(!lu.isEmpty()){
            if(lu.size() == 1){
                return lu.get(0);
            }
        }
        return null;
    }



}
